#include "CreateSphereWidget.h"
#include <gp_Pnt.hxx>

#include "Core/MainWindow.h"
#include "FeatureShape/FeatureShape.h"

CreateSphereWidget::CreateSphereWidget(MainWindow* inMainWindow, ShapeOperation* inOperation, QWidget* parent)
	: QRWCreateSolidWidget(inMainWindow, inOperation, parent)
	, ui(new Ui::CreateSphereWidgetClass())
{
	ui->setupUi(this);

	connect(ui->confirm, &QPushButton::clicked, this, &CreateSphereWidget::onConfirm);
	connect(ui->cancel, &QPushButton::clicked, this, &CreateSphereWidget::close);
	connect(ui->modify, &QPushButton::clicked, this, &CreateSphereWidget::onApply);

	connect(ui->sphereRadius, &QDoubleSpinBox::valueChanged, this, &CreateSphereWidget::onSizeChanged);

	connect(ui->pX, &QDoubleSpinBox::valueChanged, this, &CreateSphereWidget::onPosChanged);
	connect(ui->pY, &QDoubleSpinBox::valueChanged, this, &CreateSphereWidget::onPosChanged);
	connect(ui->pZ, &QDoubleSpinBox::valueChanged, this, &CreateSphereWidget::onPosChanged);

	m_originPoint = new RWTripleFloatInput(ui->pX, ui->pY, ui->pZ);
	connect(ui->selectCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &CreateSphereWidget::OnComboBoxIndexChanged);

	// operation -> param
	if (inOperation)
	{
		CreateShapeOperation* CreateOP = static_cast<CreateShapeOperation*>(inOperation);
		FeatureShapeSphere* SphereShape = static_cast<FeatureShapeSphere*>(CreateOP->m_featureShape.get());
		ui->sphereRadius->setValue(SphereShape->_radius);
		m_originPoint->SetValue(SphereShape->_position);
	}

	cacheActor = m_mainWindow->createSphere(ui->sphereRadius->value());
	onPosChanged(0.f);
}

CreateSphereWidget::~CreateSphereWidget()
{
	delete ui;
}

void CreateSphereWidget::onSizeChanged(double value)
{
	if (!cacheActor)
	{
		return;
	}
	// 这里可能不是value真的发生了变化
	cacheActor = m_mainWindow->UpdateSphereActor(cacheActor, ui->sphereRadius->value());
	onPosChanged(0.f);
}

void CreateSphereWidget::onPosChanged(double value)
{
	if (!cacheActor)
	{
		return;
	}
	cacheActor = m_mainWindow->UpdateActorPosition(cacheActor, gp_Pnt(ui->pX->value(), ui->pY->value(), ui->pZ->value()));
}

ShapeOperation* CreateSphereWidget::GetOperation()
{
	ShapeOperation* op = new CreateShapeOperation(tr("球体"), cacheActor->m_featureShape);
	return op;
}

void CreateSphereWidget::GenerateFeatureShapeForCacheActor()
{
	cacheActor->m_featureShape.reset(new FeatureShapeSphere(cacheActor->OccShape(), ui->sphereRadius->value()));
}

void CreateSphereWidget::Reset()
{
	QRWCreateSolidWidget::Reset();

    ui->sphereRadius->setValue(1.f);
	m_originPoint->SetValue(gp_Pnt());

	cacheActor = m_mainWindow->createSphere(ui->sphereRadius->value());
	onPosChanged(0.f);
}
